package com.sheng.whyclass.work.acitivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.toolbox.StringRequest;
import com.sheng.util.Constant;
import com.sheng.util.CustomToast;
import com.sheng.util.HttpProcessManager;
import com.sheng.util.LoginDataHelper;
import com.sheng.util.VolleyManager;
import com.sheng.whyclass.R;
import com.sheng.whyclass.work.event.SubmitAnswerEvent;

import de.greenrobot.event.EventBus;

public class SubmitAcitivity extends Activity implements View.OnClickListener {

    private EditText answerInput;
    private Button submitBtn;
    private Button backBtn;
    private TextView answer_title;

    private String answer;
    private String id;
    private String topic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_submit_acitivity);
        perseIntent();
        initView();
        EventBus.getDefault().register(this);
    }

    public static void startSubmitAnswerActivity(Context context, String id, String topic) {
        Intent intent = new Intent(context, SubmitAcitivity.class);
        Bundle b = new Bundle();
        b.putString(Constant.KEY_CONTENT_ID, id);
        b.putString(Constant.TOPIC, topic);

        intent.putExtras(b);
        context.startActivity(intent);
    }

    private void initView() {
        answerInput = (EditText) findViewById(R.id.answer_input);
        submitBtn = (Button) findViewById(R.id.submit_btn);
        submitBtn.setOnClickListener(this);
        backBtn = (Button) findViewById(R.id.back_btn);
        backBtn.setOnClickListener(this);
        answer_title = (TextView) findViewById(R.id.answer_title);
        answer_title.setText(topic);
    }


    private void perseIntent() {
        Intent intent = getIntent();
        if (intent == null || intent.getExtras() == null) {
            return;
        }

        Bundle b = intent.getExtras();
        id = b.getString(Constant.KEY_CONTENT_ID);
        topic = b.getString(Constant.TOPIC);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        int Vid = v.getId();
        switch (Vid) {
            case R.id.submit_btn:
                answer = answerInput.getText().toString().trim();
                submitAnswer(id, answer);
                break;
            case R.id.back_btn:
                finish();
                break;
        }
    }

    private void submitAnswer(String id, String answer) {

        String s_id = LoginDataHelper.getInstance().getLoginUserInfo().getId();

        StringRequest request = HttpProcessManager.getInstance().submitAnswer(Constant.HOST_WORK_SUBMIT, id, s_id, answer);
        VolleyManager.getInstance(getApplicationContext()).addToRequestQueue(request);
    }

    public void onEventMainThread(SubmitAnswerEvent event) {

        int code = event.code;
        if (code == Constant.CODE_FAIL) {
            CustomToast.showToast(getString(R.string.net_fail));
            return;
        } else {
            CustomToast.showToast(event.info);
            finish();
            return;
        }

    }
}
